<template>
<div class="login-container">
  <div class="login-form-wrap">
    <div class="login-head">
      <div class="logo"></div>
    </div>
    <el-form ref="form" status-icon :model="form" :rules="rules">
        <el-form-item prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入收集号码"></el-input>
        </el-form-item>
        <el-form-item prop="code" >
            <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item prop="isAgree">
            <el-checkbox v-model="form.isAgree" >我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <el-form-item>
            <el-button class="login-btn" type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
    </el-form>
  </div>
   <router-view></router-view>
</div>

</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      form: {
        // 收集表单数据
        mobile: '13911111111',
        code: '246810',
        isAgree: true
      },
      rules: {
        mobile: [
          { required: true, message: '手机号码不能为空', trigger: ['blur', 'change'] },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号码格式错误',
            trigger: ['blur', 'change']
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: ['blur', 'change'] },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误',
            trigger: ['blur', 'change']
          }
        ],
        isAgree: [
        // 自定义校验
        // rule:代表规则
        // value 当前的值
        // callback:是一个回调函数,校验完必须调用callback
        // callback()校验通过了callback(new Error('失败的提示'))校验失败
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请先同意霸王条款'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      // 对整个表单进行校验,参数是回调函数
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // console.log('校验通过了')
        try {
          const res = await this.$axios({
            // url: 'http://ttapi.research.itcast.cn/mp/v1_0/authorizations',
            url: '/mp/v1_0/authorizations',
            method: 'POST',
            data: this.form
          })
          console.log(res)
          // this.$message({
          //   message: '登录成功',
          //   type: 'success'
          // })
          // 存储token
          localStorage.setItem('token', res.data.data.token)
          this.$message.success('登录成功')
          this.$router.push('/')
        } catch (err) {
          this.$message.error('登录失败')
          console.log('登陆失败', err)
        }
      })
    }
  }
}
</script>

<style lang="less">
// 铺满整屏
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("~@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    width: 400px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("~@/assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-btn {
      width: 100%;
      margin-top: 20px;
    }
  }
}
</style>
